package com.ph.controller.personal.account;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.custom.annotation.MemberCache;
import com.ph.entity.MemberInfo;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.result.Result;
import com.ph.util.ResultUtil;

import cm.ph.shopping.facade.order.entity.PhOrderAddress;
import cm.ph.shopping.facade.order.service.IOrderService;

/**
 * @项目：phshopping
 * @描述：收货地址controller
 * @作者： Mr.zheng
 * @创建时间：2017-03-31
 * @Copyright @2017 by Mr.zheng
 */
@Controller
@RequestMapping("address")
public class AddressController {
    private static Logger logger = LoggerFactory.getLogger(AddressController.class);
    /*订单接口*/
    @Reference(version = "1.0.0")
    private IOrderService orderService;

    /**
     * 获取我的收货地址
     *
     * @author zhengpeng
     */
    @MemberCache
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String getOrderDetail(HttpServletRequest request, Map<String, Object> map,MemberInfo memberInfo) {
        try {
            //用户收货地址列表
            logger.info("查询会员收货地址列表接口入参, memberId={}",memberInfo.getId());
            Result result = orderService.queryOrderAddress(memberInfo.getId());
            logger.info("查询会员收货地址列表接口返回值, result={}", JSON.toJSONString(result));
            if (null != result && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
                map.put("userAddressList", result.getData());
            }
        } catch (Exception e) {
            logger.error("查询会员积分流水接口异常,e={}",e);
        }
        return "account/address";
    }

    /**
     * 设置默认地址
     *
     * @param id 地址Id
     * @author zhengpeng
     */
    @RequestMapping(value = "/setDefaultAddress", method = {RequestMethod.POST})
    @ResponseBody
    @MemberCache
    public Map<String, Object> setDefaultAddress(@RequestParam(value = "id") Long id, MemberInfo memberInfo, HttpServletRequest request){
        Map<String, Object> map = new HashedMap();
        ResultUtil.setResult(map,false,"设置默认地址失败");
        try {
            logger.info("设置会员默认收货地址接口入参，memberId={},addressId={}", memberInfo.getId(),id);
            Result result = orderService.updateOrderAddrDef(memberInfo.getId(),id);
            logger.info("设置会员默认收货地址接口返回值，result={},", JSON.toJSONString(result));
            if (result != null) {
                ResultUtil.setResult(map,ResultEnum.SUCCESS.getCode().equals(result.getCode()),result.getMessage() );
            }
        } catch (Exception e) {
            logger.error("设置会员默认收货地址接口异常,e={}",e);
        }
        return map;
    }

    /**
     * 删除地址
     *
     * @param id 地址Id
     * @author zhengpeng
     */
    @RequestMapping(value = "/deleteAddress", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> deleteAddress(@RequestParam(value = "id") Long id,  HttpServletRequest request){
        Map<String, Object> map = new HashedMap();
        ResultUtil.setResult(map,false,"删除地址失败");
        try {
            logger.info("删除会员收货地址接口入参，addressId={}",id);
            Result result = orderService.deleteOrderAddress(id);
            logger.info("删除会员收货地址接口返回值，result={},", JSON.toJSONString(result));
            if (result != null) {
                ResultUtil.setResult(map,ResultEnum.SUCCESS.getCode().equals(result.getCode()),result.getMessage() );
            }
        } catch (Exception e) {
            logger.error("删除会员收货地址接口异常,e={}",e);
        }
        return map;
    }

    /**
     * 新增收货地址
     *
     * @param phOrderAddress 收货地址
     * @return String
     * @author xkw
     */
    @RequestMapping(value = "/insertAddress", method = RequestMethod.POST)
    @ResponseBody
    @MemberCache
    public Map<String, Object> insertAddress(@ModelAttribute PhOrderAddress phOrderAddress, MemberInfo memberInfo,
    		HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashedMap();
        ResultUtil.setResult(map,false,"新增收货地址失败");
        try {
            phOrderAddress.setMemberId(memberInfo.getId());
            logger.info("新增收货地址接口入参，phOrderAddress={}",JSON.toJSONString(phOrderAddress));
            Result result = orderService.insertOrderAddress(phOrderAddress);
            logger.info("新增收货地址接口返回值，result={},", JSON.toJSONString(result));
            if (result != null) {
                ResultUtil.setResult(map,ResultEnum.SUCCESS.getCode().equals(result.getCode()),result.getMessage() );
            }
        } catch (Exception e) {
            logger.error("新增收货地址接口异常,e={}",e);
        }
        return map;
    }

    /**
     * 查询地址详情
     *
     * @param id 地址Id
     * @author zhengpeng
     */
    @RequestMapping(value = "/detail", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> detail(@RequestParam(value = "id") Long id,  HttpServletRequest request){
        Map<String, Object> map = new HashedMap();
        ResultUtil.setResult(map,false,"获取地址详情失败");
        try {
            logger.info("查询地址详情接口入参，addressId={}",id);
            Result result = orderService.queryAddressDetail(id);
            logger.info("查询地址详情接口返回值，result={},", JSON.toJSONString(result));
            if (result != null) {
                ResultUtil.setResult(map,ResultEnum.SUCCESS.getCode().equals(result.getCode()),result.getMessage() );
                map.put("data",result.getData());
            }
        } catch (Exception e) {
            logger.error("查询地址详情接口异常,e={}",e);
        }
        return map;
    }

    /**
     * 修改地址
     *
     * @param phOrderAddress 地址详情
     * @author zhengpeng
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> update(@ModelAttribute PhOrderAddress phOrderAddress,  HttpServletRequest request){
        Map<String, Object> map = new HashedMap();
        ResultUtil.setResult(map,false,"修改地址失败");
        try {
            logger.info("修改地址接口入参，phOrderAddress={}",JSON.toJSONString(phOrderAddress));
            Result result = orderService.updateOrderAddress(phOrderAddress);
            logger.info("修改地址接口返回值，result={},", JSON.toJSONString(result));
            if (result != null) {
                ResultUtil.setResult(map,ResultEnum.SUCCESS.getCode().equals(result.getCode()),result.getMessage() );
            }
        } catch (Exception e) {
            logger.error("修改地址接口异常,e={}",e);
        }
        return map;
    }
}
